package org.jeecg.modules.qyxx.util;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


public class DateUtils {
    private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);

    private volatile static DateUtils singleton;

    private DateUtils() {
    }

    public static DateUtils getInstance() {
        if (singleton == null) {
            synchronized (DateUtils.class) {
                if (singleton == null) {
                    singleton = new DateUtils();
                }
            }
        }
        return singleton;
    }

    /**
     * 判断一个时间是否在某个时间范围内
     *
     * @param date      参数
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return
     */
    public boolean isInDate(Date date, Date startDate, Date endDate) {
        long startTime = startDate.getTime();
        long endTime = endDate.getTime();
        long dateTime = date.getTime();
        if (startTime <= dateTime && endTime >= dateTime) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 两时间之间 倒数三年的集合
     *
     * @param kssj
     * @param jssj
     * @return
     */
    public List<String> nearlyThreeYears(Date kssj, Date jssj) {
        /* List<Date> dates = findYearDates(kssj, jssj);
        List<String> tjnds = new ArrayList<>();
        for (Date date : dates) {
            String year = getYearToStr(date);
            if (StringUtils.hasLength(year))
                tjnds.add(year);
        }
        tjnds = tjnds.subList(tjnds.size() - 3, tjnds.size());*/
        Integer ky = Integer.valueOf(getYearToStr(kssj));
        Integer jy = Integer.valueOf(getYearToStr(jssj));
        List<String> years = new ArrayList<>();
        int i = 0;
        for (; jy >= ky; jy--) {
            if (i >= 4) {
                break;
            }
            years.add(jy.toString());
            i++;
        }
        Collections.reverse(years);
        return years;
    }

    /**
     * @param date
     * @param startDate HH:mm:ss
     * @param endDate   HH:mm:ss
     * @return
     */
    public boolean isIntime(Date date, Date startDate, Date endDate) {
        boolean flag = false;
        try {
            SimpleDateFormat sdfDate = new SimpleDateFormat("HH:mm:ss");
            String dateStr = sdfDate.format(date);
            date = sdfDate.parse(dateStr);
            long startTime = startDate.getTime();
            long endTime = endDate.getTime();
            long dateTime = date.getTime();
            if (startTime == endTime) {//
                return false;
            }
            if (startTime < endTime) {
                if (startTime <= dateTime && endTime >= dateTime) {
                    flag = true;
                } else {
                    flag = false;
                }
            }
            if (startTime > endTime) {//如果开始时间是大于结束时间的话 要拆分两个段 例如 18:00:00-17:00:00 1段为18:00:00 -23:59:59 2段为00:00:00 -17:00:00
                String startStr = "00:00:00";
                String endStr = "23:59:59";
                Date startDate_1 = startDate;
                Date endDate_1 = sdfDate.parse(endStr);
                Date startDate_2 = sdfDate.parse(startStr);
                Date endDate_2 = endDate;
                if (isIntime(date, startDate_1, endDate_1) || isIntime(date, startDate_2, endDate_2)) {
                    flag = true;
                } else {
                    flag = false;
                }
            }
        } catch (Exception e) {
            logger.error("isIntime报异常", e);
            // TODO: handle exception
            flag = false;
        }
        return flag;
    }

    /**
     * 判断时间是否在时间段内
     *
     * @param nowTime
     * @param beginTime
     * @param endTime
     * @return
     */
    public boolean belongCalendar(Date nowTime, Date beginTime, Date endTime) {
        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);
        Calendar begin = Calendar.getInstance();
        begin.setTime(beginTime);
        Calendar end = Calendar.getInstance();
        end.setTime(endTime);
        if (date.after(begin) && date.before(end)) {
            return true;
        } else if (nowTime.compareTo(beginTime) == 0 || nowTime.compareTo(endTime) == 0) {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) throws ParseException {
//        System.out.println("" + DatetoStr(getCurrentQuarterStartTime(), null));
//        System.out.println("" + DatetoStr(getCurrentQuarterEndTime(), null));
//        System.out.println("" + DatetoStr(getLastMonth("2019-01-01"), null));
//        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date validStartDate = null;//有效开始时间
//        Date validEndDate = null;//有效结束时间
//
//        String startStr = "2018-11-01 00:00:00";
//        String endStr = "9999-12-31 00:00:00";
//
//        if (StringUtils.isNotBlank(startStr) && StringUtils.isNotBlank(endStr)) {
//            try {
//                validStartDate = sdfDate.parse(startStr);
//                validEndDate = sdfDate.parse(endStr);
//            } catch (Exception e) {
//                validStartDate = null;
//                validEndDate = null;
//            }
//        }
//        Date nowDate = new Date();
//        System.out.println(belongCalendar(nowDate, validStartDate, validEndDate));
        String str = "202006";
        String lastDayOfMonth = DateUtils.getInstance().getLastDayOfMonth(Integer.valueOf(str.substring(0, 4)), Integer.valueOf(str.substring(4, 6)));
        System.out.println(lastDayOfMonth);

        DateRange currentQuarter = DateUtils.getInstance().getThisQuarter();
        System.out.println("当前季度的时间范围： " + DateUtils.getInstance().format(currentQuarter.getStart()) + " - " + DateUtils.getInstance().format(currentQuarter.getEnd()));


        DateRange yesterdayRange = DateUtils.getInstance().getYesterdayRange();
        System.out.println("昨天的时间范围: " + DateUtils.getInstance().format(yesterdayRange.getStart()) + " - " + DateUtils.getInstance().format(yesterdayRange.getEnd()));

        DateRange thisMonth = DateUtils.getInstance().getThisMonth();
        System.out.println("当前月份的时间范围: " + DateUtils.getInstance().format(thisMonth.getStart()) + " - " + DateUtils.getInstance().format(thisMonth.getEnd()));

        DateRange lastQuarter = DateUtils.getInstance().getLastQuarter();
        System.out.println("上个季度的时间范围: " + DateUtils.getInstance().format(lastQuarter.getStart()) + " - " + DateUtils.getInstance().format(lastQuarter.getEnd()));

        DateRange lastMonth = DateUtils.getInstance().getLastMonthDateRange();
        String ssqq = DateUtils.getInstance().format(lastMonth.getStart());
        String ssqz = DateUtils.getInstance().format(lastMonth.getEnd());
        System.out.println("上个月的时间范围: " + ssqq + " - " + ssqz);

        System.out.println(DateUtils.getInstance().getStrDateBetween("2020-01-01", "2020-12-31"));
    }

    /**
     * 获取对应格式的时间字符串
     *
     * @param date
     * @return
     */
    public String DatetoStr(Date date, String pattern) {
        if (pattern == null) {
            pattern = "yyyy-MM-dd";
        }
        SimpleDateFormat fmt = new SimpleDateFormat(pattern);
        return fmt.format(date);
    }

    /**
     * 获取对应格式的date
     *
     * @param date
     * @return
     */
    public Date strToDate(String date, String pattern) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            if (pattern != null) {
                fmt = new SimpleDateFormat(pattern);
            }
            return fmt.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取某段时这里写代码片间内的所有日期
     *
     * @param dBegin
     * @param dEnd
     * @return
     */
    public List<Date> findDates(String dBegin, String dEnd) {
        Date sdate = DateUtils.getInstance().strToDate(dBegin, "yyyy-MM-dd");
        Date edate = DateUtils.getInstance().strToDate(dEnd, "yyyy-MM-dd");
        List<Date> lDate = new ArrayList<Date>();
        lDate.add(sdate);
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(sdate);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(edate);
        // 测试此日期是否在指定日期之后
        while (edate.after(calBegin.getTime())) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            lDate.add(calBegin.getTime());
        }
        return lDate;
    }

    /**
     * 获取某段时这里写代码片间内的所有日期
     *
     * @param sdate
     * @param edate
     * @return
     */
    public List<Date> findYearDates(Date sdate, Date edate) {
        /*Date sdate = DateUtils.getInstance().strToDate(dBegin, "yyyy-MM-dd");
        Date edate = DateUtils.getInstance().strToDate(dEnd, "yyyy-MM-dd");*/
        sdate = DateUtil.beginOfYear(sdate);
        edate = DateUtil.beginOfYear(edate);
        System.out.println(edate);
        List<Date> lDate = new ArrayList<Date>();
        lDate.add(sdate);
        Calendar calBegin = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calBegin.setTime(sdate);
        Calendar calEnd = Calendar.getInstance();
        // 使用给定的 Date 设置此 Calendar 的时间
        calEnd.setTime(edate);
        // 测试此日期是否在指定日期之后
        while (edate.after(calBegin.getTime())) {
            // 根据日历的规则，为给定的日历字段添加或减去指定的时间量
            calBegin.add(Calendar.YEAR, 1);
            lDate.add(calBegin.getTime());
        }
        return lDate;
    }

    /**
     * 获取当前年月
     *
     * @param date
     * @return
     */
    public String getMonthToStr(Date date) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMM");
        return fmt.format(date);
    }


    /**
     * 获取当前年月
     *
     * @param date
     * @return
     */
    public String getMonthToStrMM(Date date) {
        SimpleDateFormat fmt = new SimpleDateFormat("MM");
        String month = fmt.format(date);
        if (month.contains("0") && !"10".equals(month)) {
            month = month.replaceAll("0", "");
        }
        return month;
    }


    /**
     * 获取指定年月的第一天
     *
     * @param year
     * @param month
     * @return
     */
    public String getFirstDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //设置年月,日时分秒
        cal.set(year, month - 1, 1, 00, 00, 00);
        //获取某月最小天数
        int firstDay = cal.getMinimum(Calendar.DATE);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }

    public String getFirstDayOfMonth() {
        Calendar cal = Calendar.getInstance();
        //设置年月,日时分秒
//        cal.set(year, month - 1, 1, 00, 00, 00);
        //获取某月最小天数
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        cal.set(year, month - 1, 1, 00, 00, 00);
        int firstDay = cal.getMinimum(Calendar.DATE);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }

    public String getFirstDayOfMonth(Calendar cal) {
        //获取某月最小天数
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }

    public String getLastDayOfMonth(Calendar cal) {
        //获取某月最小天数
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());
    }


    /**
     * 获取指定年月的最后一天
     *
     * @param year
     * @param month
     * @return
     */
    public String getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        //隐藏bug:若只设置年月，没设置日的话，则日期是当前的天数，如设2019年9月，且系统时间是31号，则获取最大天数就是31号，而不是30号，故存在bug.解决方法是设置全日期年月日时分秒,这并不影响获取月的最后一天.
//        //设置年份
//        cal.set(Calendar.YEAR, year);
//        //设置月份
//        cal.set(Calendar.MONTH, month - 1);
//        cal.set(Calendar.DAY_OF_MONTH, 1);
        //设置年月,日时分秒
        cal.set(year, month - 1, 1, 00, 00, 00);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(cal.getTime());


    }

    public String getLastDayOfMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH);
        //隐藏bug:若只设置年月，没设置日的话，则日期是当前的天数，如设2019年9月，且系统时间是31号，则获取最大天数就是31号，而不是30号，故存在bug.解决方法是设置全日期年月日时分秒,这并不影响获取月的最后一天.
//        //设置年份
//        cal.set(Calendar.YEAR, year);
//        //设置月份
//        cal.set(Calendar.MONTH, month - 1);
//        cal.set(Calendar.DAY_OF_MONTH, 1);
        //设置年月,日时分秒
        cal.set(year, month - 1, 1, 00, 00, 00);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期

        return sdf.format(cal.getTime());


    }

    /**
     * 获取当前时间年
     *
     * @return
     */
    public int getNowYear(Calendar now) {
        return now.get(Calendar.YEAR);
    }

    /**
     * 获取当前时间月
     *
     * @return
     */
    public int getNowMonth(Calendar now) {
        return now.get(Calendar.MONTH) + 1;
    }

    /**
     * 获取n月前的Calendar
     *
     * @return
     */
    public Calendar getCalendarBeforeNMonths(int n) {
        Date dNow = new Date();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dNow);
        calendar.add(Calendar.MONTH, -n);
        return calendar;
    }

    /**
     * 获取n月前的Calendar
     * 查询start和end相差了几个月
     *
     * @return
     */
    public int getCountBeforeTwoTime(String start, String end) throws Exception {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        bef.setTime(sdf.parse(start));
        aft.setTime(sdf.parse(end));
        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
        int monthCount = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
        return result + monthCount;
    }

    /**
     * 获取n月前的Calendar
     * 查询start和end相差了几个月
     *
     * @return
     */
    public int getCountBeforeTwoTime(Date start, Date end) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        bef.setTime(start);
        aft.setTime(end);
        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
        int monthCount = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
        return result + monthCount;
    }


    /**
     * 获取n月前的Calendar
     *
     * @return
     */
    public Calendar getCalendarAfterNMonthsBySpecularTime(int n, Calendar calendar) {
        calendar.add(Calendar.MONTH, n);
        Calendar result = calendar;
        calendar.add(Calendar.MONTH, -n);
        return result;
    }


    public Date getCurrentTimeBeforeNMinutes(int n) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MINUTE, -n);// n分钟之前的时间
        Date beforeD = beforeTime.getTime();
        String time = sdf.format(beforeD);
        return beforeD;
    }

    public String getYearToStr(Date date) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy");
        return fmt.format(date);
    }

    public Date getLastMonth() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.MONTH, -1);
        return cal.getTime();
    }

    public Date getLastMonth(String now) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateFormat.parse(now)); // 设置为当前时间
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1); // 设置为上一个月
        return calendar.getTime();
    }

    public String getFourYearsAgoOneMonthFirstDay(Date now) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 4);
        calendar.set(Calendar.MONTH, calendar.getActualMinimum(Calendar.MONTH));
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        Date time = calendar.getTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(time);
    }

    public String getFourYearsAgoOneMonthLastDay(Date now) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 4);
        calendar.set(Calendar.MONTH, calendar.getActualMinimum(Calendar.MONTH));
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date time = calendar.getTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return dateFormat.format(time);
    }


    //本季度开始点时间
    public Date getCurrentQuarterStartTime(Date nowDate) {
        Calendar c = Calendar.getInstance();
        if (nowDate != null) {
            c.setTime(nowDate);
        }
        int currentMonth = c.get(Calendar.MONTH) + 1;
        SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                c.set(Calendar.MONTH, 0);
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 3);
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                c.set(Calendar.MONTH, 6);
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 9);

            }
            c.set(Calendar.DATE, 1);
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 00:00:00");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 当前季度的结束时间，即2012-03-31 23:59:59
     *
     * @return
     */
    public Date getCurrentQuarterEndTime(Date nowDate) {
        Calendar c = Calendar.getInstance();
        if (nowDate != null) {
            c.setTime(nowDate);
        }
        int currentMonth = c.get(Calendar.MONTH) + 1;
        SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat shortSdf = new SimpleDateFormat("yyyy-MM-dd");
        Date now = null;
        try {
            if (currentMonth >= 1 && currentMonth <= 3) {
                c.set(Calendar.MONTH, 2);
                c.set(Calendar.DATE, 31);
            } else if (currentMonth >= 4 && currentMonth <= 6) {
                c.set(Calendar.MONTH, 5);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 7 && currentMonth <= 9) {
                c.set(Calendar.MONTH, 8);
                c.set(Calendar.DATE, 30);
            } else if (currentMonth >= 10 && currentMonth <= 12) {
                c.set(Calendar.MONTH, 11);
                c.set(Calendar.DATE, 31);
            }
            now = longSdf.parse(shortSdf.format(c.getTime()) + " 23:59:59");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 转换字符串月份到整型
     *
     * @param month
     * @return
     */
    public Integer getIntMonth(String month) {
        if (month.contains("0") && !"10".equals(month)) {
            return Integer.valueOf(month.replace("0", ""));
        }
        return Integer.valueOf(month);
    }

    /**
     * 获取date的月份的时间范围
     *
     * @param date
     * @return
     */
    public DateRange getMonthRange(Date date) {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.setTime(date);
        startCalendar.set(Calendar.DAY_OF_MONTH, 1);
        setMaxTime(startCalendar);

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.setTime(date);
        endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        setMaxTime(endCalendar);

        return new DateRange(startCalendar.getTime(), endCalendar.getTime());
    }

    /**
     * 获取当前季度的时间范围
     *
     * @return current quarter
     */
    public DateRange getThisQuarter() {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3) * 3);
        startCalendar.set(Calendar.DAY_OF_MONTH, 1);
        setMinTime(startCalendar);

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3) * 3 + 2);
        endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        setMaxTime(endCalendar);

        return new DateRange(startCalendar.getTime(), endCalendar.getTime());
    }

    /**
     * 获取昨天的时间范围
     *
     * @return
     */
    public DateRange getYesterdayRange() {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.add(Calendar.DAY_OF_MONTH, -1);
        setMinTime(startCalendar);

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.add(Calendar.DAY_OF_MONTH, -1);
        setMaxTime(endCalendar);

        return new DateRange(startCalendar.getTime(), endCalendar.getTime());
    }

    /**
     * 获取当前月份的时间范围
     *
     * @return
     */
    public DateRange getThisMonth() {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.set(Calendar.DAY_OF_MONTH, 1);
        setMinTime(startCalendar);

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        setMaxTime(endCalendar);

        return new DateRange(startCalendar.getTime(), endCalendar.getTime());
    }

    /**
     * 获取上个月的时间范围
     *
     * @return
     */
    public DateRange getLastMonthDateRange() {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.add(Calendar.MONTH, -1);
        startCalendar.set(Calendar.DAY_OF_MONTH, 1);
        setMinTime(startCalendar);

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.add(Calendar.MONTH, -1);
        endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        setMaxTime(endCalendar);

        return new DateRange(startCalendar.getTime(), endCalendar.getTime());
    }

    /**
     * 获取上个季度的时间范围
     *
     * @return
     */
    public DateRange getLastQuarter() {
        Calendar startCalendar = Calendar.getInstance();
        startCalendar.set(Calendar.MONTH, ((int) startCalendar.get(Calendar.MONTH) / 3 - 1) * 3);
        startCalendar.set(Calendar.DAY_OF_MONTH, 1);
        setMinTime(startCalendar);

        Calendar endCalendar = Calendar.getInstance();
        endCalendar.set(Calendar.MONTH, ((int) endCalendar.get(Calendar.MONTH) / 3 - 1) * 3 + 2);
        endCalendar.set(Calendar.DAY_OF_MONTH, endCalendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        setMaxTime(endCalendar);

        return new DateRange(startCalendar.getTime(), endCalendar.getTime());
    }

    private void setMinTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
    }

    private void setMaxTime(Calendar calendar) {
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));
        calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));
        calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));
        calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));
    }


    public final String DEFAULT_PATTERN = "yyyy-MM-dd";

    public String format(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat(DEFAULT_PATTERN);
        return sdf.format(date);
    }

    public String getKpyfLastDay(String kpyf) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        int year = Integer.valueOf(kpyf.substring(0, 4));
        String m = kpyf.substring(4, 6);
        if (!"10".equals(m) && m.contains("0")) {
            m = m.replace("0", "");
        }
        int month = Integer.valueOf(m);
        //设置年月,日时分秒
        cal.set(year, month - 1, 1, 00, 00, 00);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期

        return sdf.format(cal.getTime());


    }

    public String getKpyfFirstDay(String kpyf) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        int year = Integer.valueOf(kpyf.substring(0, 4));
        String m = kpyf.substring(4, 6);
        if (!"10".equals(m) && m.contains("0")) {
            m = m.replace("0", "");
        }
        int month = Integer.valueOf(m);
        //设置年月,日时分秒
        cal.set(year, month - 1, 1, 00, 00, 00);
        //获取某月最小天数
        int firstDay = cal.getMinimum(Calendar.DATE);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期

        return sdf.format(cal.getTime());


    }

    public int getStrDateBetween(String start, String end) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");

        Calendar bef = Calendar.getInstance();
        Calendar aft = Calendar.getInstance();
        try {
            bef.setTime(sdf.parse(start));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        try {
            aft.setTime(sdf.parse(end));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        int result = aft.get(Calendar.MONTH) - bef.get(Calendar.MONTH);
        int month = (aft.get(Calendar.YEAR) - bef.get(Calendar.YEAR)) * 12;
        return Math.abs(month + result);
    }

}
